package com.kitty.game.task.service.targetHandler;

import com.kitty.game.role.model.Role;
import com.kitty.game.config.TaskSet;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.task.model.product.TaskTarget;
import com.kitty.game.task.model.product.TaskTargetType;
import com.kitty.game.task.model.product.targetparam.RoleLevelUpTargetParam;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.listener.BaseGameEvent;
import com.kitty.listener.event.RoleLevelUpEvent;

import java.util.List;

public class RoleLevelUpTargetHandler extends TaskTargetHandler {
    @Override
    public void handleEvent(Role role, BaseGameEvent baseGameEvent) {
        /**处理等级提升事件*/

        NewTaskService taskService = SpringUtils.getBean(NewTaskService.class);
        List<TaskSet> taskSetList = taskService.getAcceptedTasksMatchTarget(role, TaskTargetType.ROLE_LEVEL_UP);

        /**没有提升角色等级的任务，返回*/
        if (taskSetList.size() == 0) {return ;}

        RoleLevelUpEvent roleLevelUpEvent = (RoleLevelUpEvent)baseGameEvent;

        /**升级后的等级*/
        short upLevel = roleLevelUpEvent.getLevel();
        for (TaskSet taskSet : taskSetList) {
            TaskTarget roleDoingTaskTarget = taskService.getRoleDoingTaskTarget(role, taskSet);
            if (roleDoingTaskTarget == null) {continue; }

            RoleLevelUpTargetParam roleLevelUpTargetParam = (RoleLevelUpTargetParam)roleDoingTaskTarget.getTaskTargetParam();

            /**等级没达到时，跳过*/
            if (upLevel < roleLevelUpTargetParam.getLevel()) { continue; }

            taskService.updateTargetProgress(role, taskSet, roleDoingTaskTarget);
        }

    }

    @Override
    public void triggerNow(Role role, TaskSet taskSet, TaskTarget taskTarget) {
        /**检测角色当前等级是否已经达到目标需求等级*/
        RoleLevelUpTargetParam roleLevelUpTargetParam = (RoleLevelUpTargetParam)taskTarget.getTaskTargetParam();

        /**等级没达到时，不处理*/
        if (role.getLevel() < roleLevelUpTargetParam.getLevel()) {
            return ;
        }

        /**此时等级已达到*/
        NewTaskService taskService = SpringUtils.getBean(NewTaskService.class);
        taskService.updateTargetProgress(role, taskSet, taskTarget);
    }
}
